package cn.lg.soar.common.util.signature;

import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;

/**
 * 签名数据基础类
 * @author luguoxiang
 * @date 2022/2/18
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Getter
@Setter
public class SignBase {

    @SignIgnore
    @SignValue
    @ApiModelProperty("签名值")
    private String signValue;

    @SignField
    @ApiModelProperty("有效期")
    private Long expire;

    /**
     * 签名
     * @param se
     * @return
     */
    public void signature(String secret) {
        if (expire == null) {
            // 默认五分钟有效
            expire = SignatureUtils.createExpire(300_000L);
        }
        this.signValue = SignatureUtils.sign(secret, this);
    }

    /**
     * 验签
     * @param secret
     * @return
     */
    public boolean verification(String secret) {
        return SignatureUtils.verifyExpire(expire) && SignatureUtils.verify(signValue, secret, this);
    }

}
